﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using GameSPK.Helpers;

namespace GameSPK.DAO
{
    class PhanThiDAO
    {
        public static string getTheLe(PhanThi phanthi)
        {
            string result = "";
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                return ((from p in db.PhanThis
                          where p.MaPT == phanthi.MaPT
                          select p).Single()).TheLe.Trim();
                
            }
            catch
            {
                result = "";
            }
            return result;

        }
        public static bool setTheLe(PhanThi phanthi, string thele)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                var pt = (from p in db.PhanThis
                         where p.MaPT == phanthi.MaPT
                         select p).Single();
                pt.TheLe = thele.Trim();
                db.SubmitChanges();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public static bool setPhanThiCuocThi(General cuocthiroot, General cuocthimoi)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();

            try
            {
                var phanthis = from pts in db.PhanThi_CuocThis
                               select pts;
                foreach (PhanThi_CuocThi pt_ct in phanthis)
                {
                    PhanThi_CuocThi pt_ctnew = new PhanThi_CuocThi();
                    pt_ctnew.MaCuocChoi = cuocthimoi.MaCuocChoi;
                    pt_ctnew.MaPT = pt_ct.MaPT;
                    pt_ctnew.SoCauHoi = pt_ct.SoCauHoi;
                    pt_ctnew.ThuTu = pt_ct.ThuTu;
                    db.PhanThi_CuocThis.InsertOnSubmit(pt_ctnew);
                }
                db.SubmitChanges();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public static ObservableCollection<IPhanThi> getPhanThi()
        {
            ObservableCollection<IPhanThi> lstPhanThi = new ObservableCollection<IPhanThi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var phanthi = from pt in db.PhanThis
                         
                         select pt;
            foreach (PhanThi cphanthi in phanthi)
            {

                IPhanThi iphanthi = new dataPhanThi(cphanthi.MaPT, cphanthi.TenPT);
                lstPhanThi.Add(iphanthi);
            }
            return lstPhanThi;

        }
        public static bool setPhanThiSetup(ObservableCollection<IPhanThi> lstPhanThi)
        {
            bool result = true;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            foreach (IPhanThi phanthi in lstPhanThi)
            {
                var phanthidb = (from pt in db.PhanThis
                                where pt.MaPT == phanthi.MaPhanThi
                                select pt).Single();
                phanthidb.TenPT = phanthi.TenPhanThi;
            }
            try
            {
                db.SubmitChanges();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        public static List<PhanThi_CuocThi> getPhanThiChon(General cuocthi)
        {
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var phanchoi = (from pc in db.PhanThi_CuocThis
                            where (pc.MaCuocChoi == cuocthi.MaCuocChoi)
                            orderby pc.ThuTu
                            select pc
             );
            List<PhanThi_CuocThi> result = new List<PhanThi_CuocThi>();
            foreach (PhanThi_CuocThi pt_ct in phanchoi)
            {
                result.Add(pt_ct);
            }
            return result;
        }
        public static PhanThi getPhanThi(int mapt)
        {
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var phanchoicon = (from pcc in db.PhanThis
                               where pcc.MaPT == mapt
                               select pcc).Single();
            return phanchoicon;
        }
        public static int getSoCauHoi(int maPT, General cuocthi)
        {
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var phanchoicon = (from pcc in db.PhanThi_CuocThis
                               where (pcc.MaPT == maPT && pcc.MaCuocChoi == cuocthi.MaCuocChoi)
                               select pcc).Single();
            return (int)phanchoicon.SoCauHoi;
        }
    }
}
